package com.example.android.commitcontent.ime.cores.adapters;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class BaseRecyclerViewAdapter<D> extends RecyclerView.Adapter<BaseRecyclerViewAdapter.Holder> {

    private Context mContext;
    private int mLayoutRes;
    private OnItemClickListener<D> mOnItemClickListener;
    protected List<D> mDataList;

    public BaseRecyclerViewAdapter(Context context, int layoutRes, List<D> dataList) {
        mContext = context;
        mLayoutRes = layoutRes;
        mDataList = dataList;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Holder holder = new Holder(LayoutInflater.from(mContext).inflate(mLayoutRes, parent, false));
        if (mOnItemClickListener != null)
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(mDataList.get(holder.getAdapterPosition()), holder.getAdapterPosition());
                }
            });
        onHolderCreated(holder);
        if (holder.itemView.getBackground() == null && holder.itemView.hasOnClickListeners()&&isSetRippleBackground()) {
//            int top = holder.itemView.getPaddingTop();
//            int bottom = holder.itemView.getPaddingBottom();
//            int left = holder.itemView.getPaddingLeft();
//            int right = holder.itemView.getPaddingRight();
            TypedValue typedValue = new TypedValue();
            if (mContext.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true)) {
                holder.itemView.setBackgroundResource(typedValue.resourceId);
            }
//            holder.itemView.setPadding(left, top, right, bottom);
        }
        return holder;
    }

    protected boolean isSetRippleBackground() {
        return true;
    }

    public void setOnItemClickListener(OnItemClickListener<D> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        onBindData(mDataList.get(position), holder, position);
    }

    protected void onBindData(@NonNull D data, @NonNull Holder holder, int position) {

    }

    protected void onHolderCreated(@NonNull Holder holder) {

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public List<D> getDataList() {
        return mDataList;
    }

    public static class Holder extends RecyclerView.ViewHolder {

        private SparseArray<View> mSparseArray;

        Holder(View itemView) {
            super(itemView);
            mSparseArray = new SparseArray<>();
        }

        public Holder text(@IdRes int id, CharSequence text) {
            ((TextView) itemView.findViewById(id)).setText(text);
            return this;
        }

        public Holder image(@IdRes int id, int iconRes) {
            ((ImageView) itemView.findViewById(id)).setImageResource(iconRes);
            return this;
        }

        public Holder image(@IdRes int id, String url) {
//            ImageHelper.load(itemView.findViewById(id), url);
            return this;
        }

        public Holder click(@IdRes int id, View.OnClickListener listener) {
            findViewById(id).setOnClickListener(listener);
            return this;
        }

        @SuppressWarnings("All")
        public <T extends View> T findViewById(@IdRes int id) {
            int indexOfKey = mSparseArray.indexOfKey(id);
            if (indexOfKey >= 0)
                return (T) mSparseArray.get(id);
            View view = itemView.findViewById(id);
            mSparseArray.put(id, view);
            return (T) view;
        }

        public Holder setVisibility(int id, int visibility) {
            itemView.findViewById(id).setVisibility(visibility);
            return this;
        }
    }

    public interface OnItemClickListener<D> {
        void onItemClick(D data, int position);
    }
}
